import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';

/// 应用程序的本地化委托
class AppLocalizations {
  AppLocalizations(this.locale);

  final Locale locale;

  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations)! ??
        AppLocalizations(const Locale('zh'));
  }

  static const LocalizationsDelegate<AppLocalizations> delegate =
      _AppLocalizationsDelegate();

  static const List<LocalizationsDelegate<dynamic>> localizationsDelegates = [
    delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ];

  static const List<Locale> supportedLocales = [
    Locale('zh'), // 中文
    Locale('en'), // 英文
    Locale('fr'), // 法语
    Locale('ja'), // 日语
    Locale('ko'), // 韩语
  ];

  // 应用标题
  String get appTitle => _getMessage('appTitle');

  // 预设颜色
  String get presetColors => _getMessage('presetColors');
  String get colorWheel => _getMessage('colorWheel');
  String get settings => _getMessage('settings');

  // 预设颜色名称
  String get skinSmoothing => _getMessage('skinSmoothing');
  String get coolWhite => _getMessage('coolWhite');
  String get girlishPink => _getMessage('girlishPink');
  String get versatileLight => _getMessage('versatileLight');
  String get chickenYellow => _getMessage('chickenYellow');
  String get freshLight => _getMessage('freshLight');
  String get netPurple => _getMessage('netPurple');
  String get sunsetLight => _getMessage('sunsetLight');

  // 亮度控制
  String get lightBrightness => _getMessage('lightBrightness');
  String get screenBrightness => _getMessage('screenBrightness');

  // 相机相关
  String get takePhoto => _getMessage('takePhoto');
  String get liveMode => _getMessage('liveMode');
  String get recordVideo => _getMessage('recordVideo');
  String get timedPhoto => _getMessage('timedPhoto');

  // 设置页面
  String get language => _getMessage('language');
  String get about => _getMessage('about');
  String get savePreset => _getMessage('savePreset');
  String get deletePreset => _getMessage('deletePreset');
  String get confirmDelete => _getMessage('confirmDelete');
  String get cancel => _getMessage('cancel');
  String get confirm => _getMessage('confirm');
  String get presetName => _getMessage('presetName');
  String get save => _getMessage('save');

  // 获取本地化消息
  String _getMessage(String key) {
    switch (locale.languageCode) {
      case 'en':
        return _enMessages[key] ?? key;
      case 'fr':
        return _frMessages[key] ?? key;
      case 'ja':
        return _jaMessages[key] ?? key;
      case 'ko':
        return _koMessages[key] ?? key;
      case 'zh':
      default:
        return _zhMessages[key] ?? key;
    }
  }

  // 中文消息
  static const Map<String, String> _zhMessages = {
    'appTitle': '坚果补光灯',
    'presetColors': '预设颜色',
    'colorWheel': '色轮',
    'settings': '设置',
    'skinSmoothing': '磨皮感',
    'coolWhite': '冷白皮',
    'girlishPink': '少女感',
    'versatileLight': '百搭光',
    'chickenYellow': '小鸡黄',
    'freshLight': '清新光',
    'netPurple': '网感紫',
    'sunsetLight': '落日灯',
    'lightBrightness': '灯光亮度',
    'screenBrightness': '屏幕亮度',
    'takePhoto': '拍照',
    'liveMode': '拍Live',
    'recordVideo': '录像',
    'timedPhoto': '延时拍摄',
    'language': '语言',
    'about': '关于',
    'savePreset': '保存预设',
    'deletePreset': '删除预设',
    'confirmDelete': '确认删除',
    'cancel': '取消',
    'confirm': '确认',
    'presetName': '预设名称',
    'save': '保存',
  };

  // 英文消息
  static const Map<String, String> _enMessages = {
    'appTitle': 'Nut Fill Light',
    'presetColors': 'Preset Colors',
    'colorWheel': 'Color Wheel',
    'settings': 'Settings',
    'skinSmoothing': 'Skin Smoothing',
    'coolWhite': 'Cool White',
    'girlishPink': 'Girlish Pink',
    'versatileLight': 'Versatile Light',
    'chickenYellow': 'Chicken Yellow',
    'freshLight': 'Fresh Light',
    'netPurple': 'Net Purple',
    'sunsetLight': 'Sunset Light',
    'lightBrightness': 'Light Brightness',
    'screenBrightness': 'Screen Brightness',
    'takePhoto': 'Take Photo',
    'liveMode': 'Live Mode',
    'recordVideo': 'Record Video',
    'timedPhoto': 'Timed Photo',
    'language': 'Language',
    'about': 'About',
    'savePreset': 'Save Preset',
    'deletePreset': 'Delete Preset',
    'confirmDelete': 'Confirm Delete',
    'cancel': 'Cancel',
    'confirm': 'Confirm',
    'presetName': 'Preset Name',
    'save': 'Save',
  };

  // 法语消息
  static const Map<String, String> _frMessages = {
    'appTitle': 'Lumière d\'Appoint Noix',
    'presetColors': 'Couleurs Prédéfinies',
    'colorWheel': 'Roue Chromatique',
    'settings': 'Paramètres',
    'skinSmoothing': 'Lissage de Peau',
    'coolWhite': 'Blanc Froid',
    'girlishPink': 'Rose Féminin',
    'versatileLight': 'Lumière Polyvalente',
    'chickenYellow': 'Jaune Poulet',
    'freshLight': 'Lumière Fraîche',
    'netPurple': 'Violet Net',
    'sunsetLight': 'Lumière Coucher de Soleil',
    'lightBrightness': 'Luminosité de la Lumière',
    'screenBrightness': 'Luminosité de l\'Écran',
    'takePhoto': 'Prendre Photo',
    'liveMode': 'Mode Live',
    'recordVideo': 'Enregistrer Vidéo',
    'timedPhoto': 'Photo Chronométrée',
    'language': 'Langue',
    'about': 'À Propos',
    'savePreset': 'Enregistrer Préréglage',
    'deletePreset': 'Supprimer Préréglage',
    'confirmDelete': 'Confirmer la Suppression',
    'cancel': 'Annuler',
    'confirm': 'Confirmer',
    'presetName': 'Nom du Préréglage',
    'save': 'Enregistrer',
  };

  // 日语消息
  static const Map<String, String> _jaMessages = {
    'appTitle': 'ナッツ補助光',
    'presetColors': 'プリセットカラー',
    'colorWheel': 'カラーホイール',
    'settings': '設定',
    'skinSmoothing': '肌スムージング',
    'coolWhite': 'クールホワイト',
    'girlishPink': '少女ピンク',
    'versatileLight': '万能ライト',
    'chickenYellow': 'チキンイエロー',
    'freshLight': 'フレッシュライト',
    'netPurple': 'ネットパープル',
    'sunsetLight': '夕日ライト',
    'lightBrightness': 'ライトの明るさ',
    'screenBrightness': '画面の明るさ',
    'takePhoto': '写真を撮る',
    'liveMode': 'ライブモード',
    'recordVideo': 'ビデオ録画',
    'timedPhoto': 'タイマー撮影',
    'language': '言語',
    'about': 'について',
    'savePreset': 'プリセットを保存',
    'deletePreset': 'プリセットを削除',
    'confirmDelete': '削除の確認',
    'cancel': 'キャンセル',
    'confirm': '確認',
    'presetName': 'プリセット名',
    'save': '保存',
  };

  // 韩语消息
  static const Map<String, String> _koMessages = {
    'appTitle': '너트 보조 조명',
    'presetColors': '프리셋 색상',
    'colorWheel': '색상 휠',
    'settings': '설정',
    'skinSmoothing': '피부 스무딩',
    'coolWhite': '쿨 화이트',
    'girlishPink': '소녀 핑크',
    'versatileLight': '다용도 조명',
    'chickenYellow': '치킨 옐로우',
    'freshLight': '프레시 라이트',
    'netPurple': '네트 퍼플',
    'sunsetLight': '선셋 라이트',
    'lightBrightness': '조명 밝기',
    'screenBrightness': '화면 밝기',
    'takePhoto': '사진 촬영',
    'liveMode': '라이브 모드',
    'recordVideo': '비디오 녹화',
    'timedPhoto': '타이머 촬영',
    'language': '언어',
    'about': '정보',
    'savePreset': '프리셋 저장',
    'deletePreset': '프리셋 삭제',
    'confirmDelete': '삭제 확인',
    'cancel': '취소',
    'confirm': '확인',
    'presetName': '프리셋 이름',
    'save': '저장',
  };
}

class _AppLocalizationsDelegate
    extends LocalizationsDelegate<AppLocalizations> {
  const _AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['zh', 'en', 'fr', 'ja', 'ko'].contains(locale.languageCode);
  }

  @override
  Future<AppLocalizations> load(Locale locale) {
    return SynchronousFuture<AppLocalizations>(AppLocalizations(locale));
  }

  @override
  bool shouldReload(_AppLocalizationsDelegate old) => false;
}
